home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / obj2asm.zip / OUFIND.C < prev    next >
Text File  |  1991-10-02  |  2KB  |  53 lines

  1. #include <stdio.h>
  2. #include "o.h"
  3.  
  4. /*
  5.  char   *data;                       pointer to data struct to find
  6.  NODE_T *root_node;                  pointer to root node
  7.  int    (*cmp_routine)(void*,void*); routine used to compare values at 2 nodes
  8.  NODE_T **node_ptr;                  Ptr to node found
  9. */
  10.  
  11. void *find( void *data, NODE_T *root_node, int(*cmp_routine)(void*,void*), NODE_T **node_ptr)
  12. {
  13.     NODE_T  *curr_node;
  14.     NODE_T  *child_node;
  15.     int     curr_direct;
  16.  
  17.     /* Prepare initial value for tree traversal */
  18.     child_node = root_node;
  19.  
  20.     /*
  21.     **  Traverse tree looking for the place to insert this new node.  If
  22.     **  we find a node which gives a comparison result of 0 (equal), then
  23.     **  we cannot insert this new node.  To indicate this duplicate value,
  24.     **  a non-zero value is returned (the value happens to be a pointer to
  25.     **  node which contains the duplicate value).
  26.     */
  27.     do  {
  28.         curr_node = child_node;
  29.  
  30.         /*  Compare this new node with the node at this position in the
  31.         **  tree and determine which direction to proceed from here.
  32.         */
  33.         curr_direct = (* cmp_routine)( curr_node->data, data );
  34.  
  35.         /*  is this node is a duplicate node?
  36.         */
  37.         if ( node_ptr && curr_direct == RIGHT ) {
  38.             *node_ptr = curr_node;
  39.         }
  40.         if ( curr_direct == EQUAL ) {
  41.             if ( node_ptr ) {
  42.                 *node_ptr = curr_node;
  43.             }
  44.             return ( curr_node->data );
  45.         }
  46.  
  47.         child_node = curr_node->ptr[curr_direct];
  48.  
  49.     } while ( !curr_node->thread[curr_direct]);
  50.  
  51.     return( NULL );
  52. }
  53.